package duyd.transaction.training;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

import org.springframework.context.support.GenericXmlApplicationContext;

import duyd.transaction.training.domain.Contact;
import duyd.transaction.training.service.ContactService;

public class TxDeclarativeExample {

    public static void main(String[] args) {
        GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
        ctx.load("tx-declarative-app-context.xml");
        ctx.refresh();

        ContactService contactService = ctx.getBean("contactService", ContactService.class);
        List<Contact> contacts = contactService.findAll();
        for (Contact contact : contacts) {
            System.out.println(contact);
        }

        contacts = contactService.findByFirstNameAndLastName("Clarence", "Ho");
        for (Contact contact : contacts) {
            System.out.println(contact);
        }

        try {
            contacts = contactService.findByBirthDate(new SimpleDateFormat("yyyy-MM-dd").parse("1980-07-30"));
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        for (Contact contact : contacts) {
            System.out.println(contact);
        }

        Contact contact = contactService.findOne(1L);
        contact.setFirstName("modified");
        contactService.save(contact);
        System.out.println("Contact count: " + contactService.countAll());

    }
}
